import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/:pathMatch(.*)*',
      redirect: '/'
    },
    {
      path: '/',
      component: () => import('@/views/Home/index.vue')
    },
    {
      path: '/post',
      component: () => import('@/views/Post/index.vue')
    },
    {
      path: '/album',
      component: () => import('@/views/Album/index.vue')
    }
  ],
  scrollBehavior() {
    return { top: 0 }
  }
})
router.beforeEach((to, from, next) => {
  const userStore = useUserStore()
  if (to.path == '/') {
    next()
  } else if (userStore.token) {
    next()
  } else {
    next('/')
  }
})

export default router
